//package com.example.demo.conf;
//
//import cn.hutool.core.io.FileUtil;
//import cn.hutool.core.util.StrUtil;
//import com.github.tobato.fastdfs.service.FastFileStorageClient;
//import com.github.tobato.fastdfs.service.TrackerClient;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.Validate;
//import org.csource.common.MyException;
//import org.csource.fastdfs.*;
//
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Properties;
//
///**
// * <pre>
// *  描述: Fast DFS 客户端操作类
// * </pre>
// *
// * @author godelgnis(wujiaer)
// * @version 1.0.0
// * @date 2022/3/9 11:38
// */
//@Slf4j
//public class FastStorageClient {
//
//    private TrackerClient trackerClient;
//
//    private static final int SUCCESS_UPLOAD_ARG_LENGTH = 2;
//
//    private FastStorageClient() {
//        init();
//    }
//
//    public static FastStorageClient getInstance() {
//        return Client.INSTANCE;
//    }
//
//    private static class Client{
//        private static final FastStorageClient INSTANCE = new FastStorageClient();
//    }
//
//    private void init (){
//        HashMap<String, Object> env = new HashMap<>();
//        env.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, "192.168.54.21:21212");
//
//        Properties properties = new Properties();
//        try {
//            setConfigProperty(properties, env, ClientGlobal.PROP_KEY_CHARSET, false);
//            setConfigProperty(properties, env, ClientGlobal.PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS, false);
//            setConfigProperty(properties, env, ClientGlobal.PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS, false);
//
//            setConfigProperty(properties, env, ClientGlobal.PROP_KEY_CONNECTION_POOL_ENABLED, false);
//            setConfigProperty(properties, env, ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY, false);
//            setConfigProperty(properties, env, ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME, false);
//            setConfigProperty(properties, env, ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS, false);
//
//            setConfigProperty(properties, env, ClientGlobal.PROP_KEY_HTTP_ANTI_STEAL_TOKEN, false);
//            setConfigProperty(properties, env, ClientGlobal.PROP_KEY_HTTP_SECRET_KEY, false);
//            setConfigProperty(properties, env, ClientGlobal.PROP_KEY_HTTP_TRACKER_HTTP_PORT, false);
//            setConfigProperty(properties, env, ClientGlobal.PROP_KEY_TRACKER_SERVERS, true);
//
//            ClientGlobal.initByProperties(properties);
//            trackerClient = new TrackerClient();
//        } catch (IOException | MyException e) {
//            log.error("Fast DFS 客户端初始化失败，请检查 Fast DFS 配置信息");
//        }
//    }
//
//    public FileInfo queryFileInfo(String fullPath) {
//        StorePath storePath = StorePath.parseFromUrl(fullPath);
//        return queryFileInfo(storePath);
//    }
//
//    public FileInfo queryFileInfo(StorePath storePath) {
//        return queryFileInfo(storePath.getGroup(), storePath.getRemoteFileName());
//    }
//
//    public FileInfo queryFileInfo(String group, String fileName) {
//        TrackerServer trackerServer;
//        try {
//            trackerServer = trackerClient.getTrackerServer();
//        } catch (IOException e) {
//            log.error("get tracker server error, group: {}, fileName: {}, error: {}", group, fileName, e.getMessage());
//            return null;
//        }
//        StorageClient storageClient = new StorageClient(trackerServer);
//        try {
//            return storageClient.query_file_info(group, fileName);
//        } catch (IOException | MyException e) {
//            log.error("query file info error, group: {}, path: {}, error: {}", group, fileName, e.getMessage());
//            return null;
//        } finally {
//            closeTrackerServer(trackerServer);
//        }
//    }
//
//    /**
//     * 向FastDFS上传文件
//     * @param fileContent 文件内容
//     * @param fileExtName 文件扩展名，比如 png, txt 等
//     * @return 上传成功，返回组名和该文件在FastDFS中的虚拟路径；上传失败，返回null
//     */
//    public StorePath uploadFile(String fileContent, String fileExtName) {
//        Validate.notBlank(fileContent, "文件内容不能为空");
//        return uploadFile(fileContent.getBytes(), fileExtName);
//    }
//
//
//    /**
//     * 向FastDFS上传文件
//     * @param fileBytes 本地文件名
//     * @param fileExtName 文件扩展名，比如 png, txt 等
//     * @return 上传成功，返回组名和该文件在FastDFS中的虚拟路径；上传失败，返回null
//     */
//    public StorePath uploadFile(byte[] fileBytes, String fileExtName) {
//        TrackerServer trackerServer;
//        try {
//            trackerServer = trackerClient.getTrackerServer();
//        } catch (IOException e) {
//            log.error("get tracker server error: {}", e.getMessage());
//            return null;
//        }
//        StorageClient storageClient = new StorageClient(trackerServer);
//        try {
//            String[] arr = storageClient.upload_file(fileBytes, fileExtName, null);
//            if (arr == null || arr.length != SUCCESS_UPLOAD_ARG_LENGTH) {
//                log.error("向FastDFS上传文件失败");
//                return null;
//            } else {
//                log.info("向FastDFS上传文件成功");
//                return new StorePath(arr[0], arr[1]);
//            }
//        } catch (IOException | MyException e) {
//            log.error("error", e);
//            return null;
//        } finally {
//            closeTrackerServer(trackerServer);
//        }
//    }
//
//    public boolean deleteFile(String fullPath) {
//        StorePath storePath = StorePath.parseFromUrl(fullPath);
//        return deleteFile(storePath);
//    }
//
//    public boolean deleteFile(StorePath storePath) {
//        return deleteFile(storePath.getGroup(), storePath.getRemoteFileName());
//    }
//
//    public boolean deleteFile(String group, String fileName) {
//        TrackerServer trackerServer;
//        try {
//            trackerServer = trackerClient.getTrackerServer();
//        } catch (IOException e) {
//            log.error("get tracker server error: {}", e.getMessage());
//            return false;
//        }
//        StorageClient storageClient = new StorageClient(trackerServer);
//        try {
//            int code = storageClient.delete_file(group, fileName);
//            if (code == 0) {
//                log.info("删除FastDFS文件成功, group: {}, fileName: {}", group, fileName);
//                return true;
//            } else {
//                log.error("删除FastDFS文件失败, error code: {}, group: {}, fileName: {}", code, group, fileName);
//                return false;
//            }
//        } catch (IOException | MyException e) {
//            log.error("error", e);
//            return false;
//        } finally {
//            closeTrackerServer(trackerServer);
//        }
//    }
//
//
//
//    private void closeTrackerServer(TrackerServer trackerServer) {
//        try {
//            if (trackerServer != null) {
//                trackerServer.getConnection().close();
//            }
//        } catch (IOException | MyException e) {
//            log.error("关闭trackerServer连接异常, error: {}", e.getMessage());
//        }
//    }
//
//    private void setConfigProperty(Properties properties, HashMap<String, Object> env, String property, boolean isNecessary) {
//        String configValue;
//        try {
//            Object value = env.get(property);
//            if (value == null) {
//                // 属性值为空，并且是必要属性
//                if (isNecessary) {
//                    log.error("缺少必要配置属性：{}", property);
//                }
//                return;
//            }
//            configValue = String.valueOf(value);
//        } catch (Exception e) {
//            log.error("解析配置文件属性：{} 异常，请检查相关配置，error：{}", property, e.getMessage());
//            return;
//        }
//        if (StrUtil.isNotEmpty(configValue)) {
//            properties.put(property, configValue);
//            return;
//        }
//        // 属性值为空，并且是必要属性
//        if (isNecessary) {
//            log.error("缺少必要配置属性：{}", property);
//        }
//
//    }
//
//    public static void main(String[] args) {
//        System.out.println("#".compareTo("+"));
//        //FastStorageClient instance = FastStorageClient.getInstance();
//        //byte[] bytes = FileUtil.readBytes(new File("C:\\Users\\20200717019\\Pictures\\埃罗芒阿\\壁纸\\1.jpg"));
//        //StorePath storePath = instance.uploadFile(bytes, "jpg");
//        //String fullPath = storePath.getFullPath();
//        //log.info("文件路径：{}", fullPath);
//        //FileInfo fileInfo = instance.queryFileInfo(fullPath);
//        //log.info("文件信息：{}",fileInfo.toString());
//        //boolean b = instance.deleteFile(fullPath);
//        //log.info("文件删除结果：{}", b);
//    }
//}
